package commEntity

import (
	"encoding/json"
)

type DeviceModelEntity struct {
	BaseEntity *BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名
	ModelName    string // 模板名称

	ModelParam  map[string]interface{} // 配置参数
	ExtendParam map[string]interface{} // 扩展参数
}

type deviceModelPo struct {
	Id           int64                  `json:"id" db:"id"`                     // 对象id
	CreateTime   int64                  `json:"createTime" db:"create_time"`    // 创建时间
	UpdateTime   int64                  `json:"updateTime" db:"update_time"`    // 更新时间
	Manufacturer string                 `json:"manufacturer" db:"manufacturer"` // 设备厂商名称
	DeviceType   string                 `json:"deviceType" db:"device_type"`    // 设备类型名
	ModelName    string                 `json:"modelName" db:"model_name"`      // 模板名称
	ModelParam   map[string]interface{} `json:"modelParam" db:"model_param"`    // 配置参数
	ExtendParam  map[string]interface{} `json:"extendParam" db:"extend_param"`  // 扩展参数
}

func (e *DeviceModelEntity) Type() string {
	return "DeviceModelEntity"
}

func (e *DeviceModelEntity) TableName() string {
	return "tb_device_model"
}

func (e *DeviceModelEntity) GetBaseEntity() *BaseEntity {
	return e.BaseEntity
}

func (e *DeviceModelEntity) Instance() *DeviceModelEntity {
	return &DeviceModelEntity{
		BaseEntity:  new(BaseEntity),
		ModelParam:  make(map[string]interface{}),
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *DeviceModelEntity) NewEntity() IEntity {
	return e.Instance()
}

func (e *DeviceModelEntity) NewPo() interface{} {
	return &deviceModelPo{
		ModelParam:  make(map[string]interface{}),
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *DeviceModelEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return MakeServiceKey(p)
}

func (e *DeviceModelEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.ModelName)
	return p
}

func (e *DeviceModelEntity) BuildByJson(jsn *string) error {
	var po deviceModelPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceModelEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceModelEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*deviceModelPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.ModelParam = po.ModelParam
	ptr.ExtendParam = po.ExtendParam
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.ModelName = po.ModelName
}

func (e *DeviceModelEntity) BuildToPo() interface{} {
	ptr := e
	po := deviceModelPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.ModelParam = ptr.ModelParam
	po.ExtendParam = ptr.ExtendParam
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.ModelName = ptr.ModelName

	return &po
}
